// pages/shareapp/share.js
var share=require('./model/ShareModel.js')
const QRCode = require('../../utils/qrcode/weapp-qrcode.js')
import rpx2px from '../../utils/qrcode/rpx2px.js'
let qrcode;
const qrcodeWidth = rpx2px(300)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareInfo: { content: '', ak: '', nk: '', rewardSharedCount: '', rewardTargetCount: '', timeStamp: '', sn:''},
    shareUrl:'',
    shareAk:'',
    qrcodeWidth: qrcodeWidth
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    share.getShareInfo(function(data){
      that.setData({
        shareInfo:data,
        shareAk:data.ak
      })
      that.getShareUrl()
    })
  },
  getShareUrl(){
    var obj = this.data.shareInfo
    //打开小程序路径，需要在微信后台配置
    var _url = 'http://test.fenbibox.com/?shareAk=' + obj.ak + "&award=" + obj.rewardTargetCount
    console.log(_url)
    this.setData({
      shareUrl:_url
    })
    wx.showLoading({
      title: '请稍等...',
    })
    qrcode = new QRCode('canvas', {
      // usingIn: this,
      text: _url,
      // image: '/images/bg.jpg',
      width: qrcodeWidth,
      height: qrcodeWidth,
      colorDark: "#000000",
      colorLight: "white",
      correctLevel: QRCode.CorrectLevel.H,
    });
    wx.hideLoading()
  },
  goMyShareList:function(event){
    wx.navigateTo({
      url: '../sharelist/list',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var obj = this.data.shareInfo
    var _url = "../login/login?shareAk=" + obj.ak + "&award=" + obj.rewardTargetCount
    var shareObject = {
      title: "【叮咚】您的好友"+obj.nk+"给您发了一个红包，点击马上领取。",
      path: 'pages/login/login?shareAk=' + obj.ak+"&award="+obj.rewardTargetCount,
      imageUrl: "http://test.fenbibox.com/chalkh5/img/open-no.png",
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          icon: ''
        })
      }
    }
    return shareObject;
  }
})